package com.example.myapplication.HomeRecyclerViewAdapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.HomeRecyclerViewFragment.HomeRecyclerViewBean.JingPinKeChengBean;
import com.example.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class JingPinKeChengAdapter extends RecyclerView.Adapter<JingPinKeChengAdapter.ViewHolder>{
    List<JingPinKeChengBean> list=new ArrayList<>();
    static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView img;
        TextView zhuangtai,name;
        //用于储存解析到的View
        View view;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            //
            view=itemView;
            //绑定控件
            img=view.findViewById(R.id.JingPinKeCheng_img);
            zhuangtai=view.findViewById(R.id.JingPinKeCheng_text1);
            name=view.findViewById(R.id.JingPinKeCheng_text2);
        }
    }
    //生成用于接收的构造方法


    public JingPinKeChengAdapter(List<JingPinKeChengBean> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.jingpinkecheng_mokuai,parent,false);
        //创建一个ViewHolder绑定解析到的View
        final ViewHolder viewHolder=new ViewHolder(view);
        //添加点击事件
        viewHolder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //应该是获取一个值赋值给position？
                int position=viewHolder.getAdapterPosition();
                //将获取的数据单元赋值给JingPinKeChengBean类？
                JingPinKeChengBean jingPinKeChengBean=list.get(position);
                //获取数据冰输出
                Toast.makeText(view.getContext(),jingPinKeChengBean.getImg(),Toast.LENGTH_LONG).show();
                Toast.makeText(view.getContext(),jingPinKeChengBean.getZhangtai(),Toast.LENGTH_LONG).show();
                Toast.makeText(view.getContext(),jingPinKeChengBean.getName(),Toast.LENGTH_LONG).show();

            }
        });
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        JingPinKeChengBean jingPinKeChengBean=list.get(position);
        holder.img.setImageResource(jingPinKeChengBean.getImg());
        holder.zhuangtai.setText(jingPinKeChengBean.getZhangtai());
        holder.name.setText(jingPinKeChengBean.getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
}
